<template>
  <div id="mesproductioninfob-add-or-update">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="产品名称" prop="productionName">
        <el-input v-model="dataForm.productionName" placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-select v-model="dataForm.enabledFlg" placeholder="是否启用" clearable>
          <el-option
            v-for="item in optionsFlg"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品规格">
        <el-input v-model="dataForm.productionSpecification" placeholder="产品规格"></el-input>
      </el-form-item>
      <el-form-item label="产品型号">
        <el-input v-model="dataForm.productionModel" placeholder="产品型号"></el-input>
      </el-form-item>
      <el-form-item label="产品分类">
        <el-select v-model="dataForm.productionType" placeholder="产品分类" clearable>
          <el-option
            v-for="item in optionsType"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品单位">
        <el-select v-model="dataForm.productionUnit" placeholder="产品单位" clearable>
          <el-option
            v-for="item in optionsUnit"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二维码">
        <el-input v-model="dataForm.productionQrcode" placeholder="二维码"></el-input>
      </el-form-item>
      <el-form-item label="二维码路径">
        <el-input v-model="dataForm.productionQrcodePath" placeholder="二维码路径"></el-input>
      </el-form-item>
      <el-form-item label="材质">
        <el-input v-model="dataForm.materialTexture" placeholder="材质"></el-input>
      </el-form-item>
      <el-form-item label="单重">
        <el-input v-model="dataForm.singleWeight" placeholder="单重"></el-input>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
      <el-button @click="cancle">取消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        productionNum: '',
        productionName: '',
        enabledFlg: '',
        productionSpecification: '',
        productionModel: '',
        productionType: '',
        productionUnit: '',
        productionQrcode: '',
        productionQrcodePath: '',
        materialTexture:'',
        singleWeight:''
      },
      optionsFlg: [],
      optionsType: [],
      optionsUnit: [],
      dataRule: {
        productionName: [
          { required: true, message: '产品名称不能为空', trigger: 'blur' }
        ],
        enabledFlg: [
          { required: true, message: '是否启用不能为空', trigger: 'blur' }
        ],
        productionSpecification: [
          { required: true, message: '产品规格不能为空', trigger: 'blur' }
        ],
        productionModel: [
          { required: true, message: '产品型号不能为空', trigger: 'blur' }
        ],
        productionType: [
          { required: true, message: '产品分类不能为空', trigger: 'blur' }
        ],
        productionUnit: [
          { required: true, message: '产品单位不能为空', trigger: 'blur' }
        ],
        productionQrcode: [
          { required: true, message: '产品二维码不能为空', trigger: 'blur' }
        ],
        productionQrcodePath: [
          {required: true,message: '产品二维码存放路径不能为空',trigger: 'blur'}
        ],
        materialTexture: [
          { required: true, message: '材质不能为空', trigger: 'blur' }
        ],
        singleWeight: [
          { required: true, message: '单重不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.optionLists()
    this.init(this.$route.params.id)
  },
  methods: {
    init(id) {
      this.dataForm.productionNum = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.productionNum) {
          this.$http({
            url: this.$http.adornUrl(
              `/basic/mesproductioninfob/info/${this.dataForm.productionNum}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.mesProductionInfoB
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/basic/mesproductioninfob/${
                !this.dataForm.productionNum ? 'save' : 'update'
              }`
            ),
            method: 'post',
            data: this.$http.adornData({
              productionNum: this.dataForm.productionNum || undefined,
              productionName: this.dataForm.productionName,
              enabledFlg: this.dataForm.enabledFlg,
              productionSpecification: this.dataForm.productionSpecification,
              productionModel: this.dataForm.productionModel,
              productionType: this.dataForm.productionType,
              productionUnit: this.dataForm.productionUnit,
              productionQrcode: this.dataForm.productionQrcode,
              productionQrcodePath: this.dataForm.productionQrcodePath,
              materialTexture: this.dataForm.materialTexture,
              singleWeight: this.dataForm.singleWeight
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  // this.visible = false
                  // this.$emit('refreshDataList')
                  eventBus.$emit('closeCurrentPage')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    optionLists() {
      let arr = ['unit', 'product_type', 'sys_yes_no']
      this.$http({
        url: this.$http.adornUrl('/basic/mescodemasterm/dataList'),
        method: 'post',
        data: this.$http.adornParams(arr, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          data.sys_yes_no.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.optionsFlg.push(obj)
          })
          data.unit.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.optionsUnit.push(obj)
          })
          data.product_type.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.optionsType.push(obj)
          })
        } else {
        }
        this.dataListLoading = false
      })
    },
    // 取消
    cancle() {
      eventBus.$emit('closeCurrentPage')
    }
  }
}
</script>
<style scoped lang="scss">
#mesproductioninfob-add-or-update {
  width: 60%;
  margin: 0 auto;
  .el-form-item {
    .el-select {
      width: 500px;
    }
    .el-input {
      width: 500px;
    }
  }
  .footer {
    text-align: center;
  }
}
</style>